package com.zr.d_c.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.d_c.dao.BuyCartDao;
import com.zr.d_c.dao.DishDao;
import com.zr.d_c.dao.SellerDao;
import com.zr.d_c.model.BuyCart;
import com.zr.d_c.model.Dish;
import com.zr.d_c.model.Seller;
import com.zr.d_c.util.JDBCUtil;

public class BuyCartDaoImpl implements BuyCartDao {

	DishDao dishdao = new DishDaoImpl();
	SellerDao sellerdao = new SellerDaoImpl();
	@Override
	public List<BuyCart> selectBuyCart(int cid, int start, int pageSize) {
		// TODO Auto-generated method stub
		List<BuyCart> buycarts = new ArrayList<BuyCart>();
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("select * from d_cbuycart where cid = ? order by did desc limit ?,?");
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, cid);
			pst.setInt(2, start);
			pst.setInt(3, pageSize);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				BuyCart b = new BuyCart();
				b.setBid(rs.getInt("bid"));
				b.setBnum(rs.getInt("bnum"));
				b.setCid(rs.getInt("cid"));
				b.setDid(rs.getInt("did"));
				buycarts.add(b);
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return buycarts;
	}

	@Override
	public int deleteBuyCartByBid(int bid) {
		// TODO Auto-generated method stub
		int flag = 0;
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("delete from d_cbuycart where bid = ?");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, bid);
			int i = pst.executeUpdate();
			if(i > 0){
				flag = 1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return flag;
	}

	@Override
	public int selectCountDishOfBuyCartByCid(int cid) {
		// TODO Auto-generated method stub
		int count = 0;
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("select count(*) countNum from d_cbuycart where cid = ?");
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, cid);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				count = rs.getInt("countNum");
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return count;
	}

	@Override
	public int updateBuyCartByBid(int bnum, int bid) {
		// TODO Auto-generated method stub
		int flag = 0;
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("update d_cbuycart set bnum = ? where bid = ?");
		
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, bnum);
			pst.setInt(2, bid);
			int i = pst.executeUpdate();
			if(i > 0){
				flag = 1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return flag;
	}

	@Override
	public BuyCart selectBuyCartByBid(int bid) {
		// TODO Auto-generated method stub
		BuyCart b = new BuyCart();
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("select * from d_cbuycart where bid = ?");
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, bid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){	
				b.setBid(rs.getInt("bid"));
				b.setBnum(rs.getInt("bnum"));
				b.setCid(rs.getInt("cid"));
				b.setDid(rs.getInt("did"));
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return b;
	}

	@Override
	public List<BuyCart> selectBuyCartAndSname(int cid, int start, int pageSize, String sname) {
		// TODO Auto-generated method stub
		List<BuyCart> buycarts = new ArrayList<BuyCart>();
		Connection con = null;
		PreparedStatement pst = null;
		
		StringBuffer sql = new StringBuffer("select * from d_cbuycart where cid = ? order by did desc limit ?,?");
		try {
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, cid);
			pst.setInt(2, start);
			pst.setInt(3, pageSize);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				int did = rs.getInt("did");
				Dish d = dishdao.selectDishByDid(did);
				int sid = d.getSid();
				Seller s = sellerdao.selectSellerBySid(sid);
				String ssname = s.getSname();
				if(ssname.equals(sname)){
					BuyCart b = new BuyCart();
					b.setBid(rs.getInt("bid"));
					b.setBnum(rs.getInt("bnum"));
					b.setCid(rs.getInt("cid"));
					b.setDid(rs.getInt("did"));
					buycarts.add(b);
				}
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeJDBC(pst, con);
		}
		return buycarts;
	}

}
